package com.panda.web.service.commons.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.panda.web.domain.dto.TrendDTO;
import com.panda.web.domain.dto.commons.FeedbackDTO;
import com.panda.web.domain.vo.commons.BackGroundVO;
import com.panda.web.domain.vo.commons.FeedbackVO;
import com.panda.web.domain.vo.commons.TrendVO;
import com.panda.web.domain.vo.wechatinfo.UserinfoVO;
import com.panda.web.entity.commons.FeedbackPicture;
import com.panda.web.entity.commons.WebFeedback;
import com.panda.web.entity.product.Product;
import com.panda.web.entity.wechat.WechatUser;
import com.panda.web.entity.wechatinfo.WechatUserinfo;
import com.panda.web.mapper.commons.FeedbackPictureMapper;
import com.panda.web.mapper.commons.WebFeedbackMapper;
import com.panda.web.mapper.order.WechatOrderMapper;
import com.panda.web.mapper.product.OpenidProductMapper;
import com.panda.web.mapper.product.ProductMapper;
import com.panda.web.mapper.py.JobMapper;
import com.panda.web.mapper.wechat.WechatUserMapper;
import com.panda.web.mapper.wechatinfo.WechatUserinfoMapper;
import com.panda.web.service.commons.BackgroundService;
import com.panda.web.service.commons.WebFeedbackService;
import com.panda.web.utils.pageutils.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Slf4j
@Service
public class WebFeedbackServiceImpl implements WebFeedbackService {

    @Autowired
    private WechatUserMapper wechatUserMapper;
    @Autowired
    private WebFeedbackMapper feedbackMapper;
    @Autowired
    private FeedbackPictureMapper pictureMapper;

    /**
     * 分页查询
     * @param dto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResult<FeedbackVO> PageList(FeedbackDTO dto, Integer pageNum, Integer pageSize) {
        Page<WebFeedback> page = new Page<>(pageNum, pageSize);
        QueryWrapper<WebFeedback> queryWrapper = new QueryWrapper();//需要处理查询条件

        if (!Objects.equals(dto.getFeedbackMk(),null)){
            queryWrapper.like("feedback_mk",dto.getFeedbackMk());
        }

        queryWrapper.orderByDesc("create_time");
        IPage<WebFeedback> feedbackIPage = feedbackMapper.selectPage(page, queryWrapper);
        long total = feedbackIPage.getTotal();
        List<WebFeedback> records = feedbackIPage.getRecords();

        List<FeedbackVO> voList = feedbackVoToList(records);//封装vo数据方法

        return new PageResult<>(voList, total, pageNum, pageSize);
    }

    /**
     * 新增反馈模块
     * @param dto
     * @return
     */
    @Override
    public int insterFeedback(FeedbackDTO dto) {
        WebFeedback feedback = new WebFeedback();
        BeanUtils.copyProperties(dto,feedback);
        feedback.setCreateTime(new Date());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String stringTime = format.format(new Date());
        feedback.setCreationTime(stringTime);
        WechatUser wechatUser = wechatUserMapper.findByOpenId(dto.getOpenid());
        feedback.setNickName(wechatUser.getNickname());
        feedback.setHeadimgurl(wechatUser.getHeadimgurl());
        feedbackMapper.insert(feedback);
        List<String> urlList = dto.getUrlList();
        if (urlList!=null&&!urlList.isEmpty()){
            for (String url : urlList) {
                FeedbackPicture feedbackPicture = new FeedbackPicture();
                feedbackPicture.setFeedbackId(feedback.getFeedbackId());
                feedbackPicture.setUrl(url);
                pictureMapper.insert(feedbackPicture);
            }
        }
        return 1;
    }

    /**
     * 封装返回数据
     * @param feedbackList
     * @return
     */
    private List<FeedbackVO> feedbackVoToList(List<WebFeedback> feedbackList) {
        List<FeedbackVO> voList = new ArrayList<>();
        if (feedbackList.size() > 0 && !feedbackList.isEmpty()) {
            for (WebFeedback feedback : feedbackList) {
                FeedbackVO vo = new FeedbackVO();
                BeanUtils.copyProperties(feedback, vo);
               List<String> urlList =  pictureMapper.findByFeedbackId(feedback.getFeedbackId());
               if (urlList!=null&&!urlList.isEmpty()){
                   vo.setUrlList(urlList);
               }else {
                   vo.setUrlList(new ArrayList<>());
               }

                voList.add(vo);
            }
        }
        return voList;
    }
}
